package javaCode;

/**
 * Created by lxy_920912 on 02/05/2017.
 */
public class LeetCode463 {
    public int islandPerimeter(int[][] grid) {
        int count = 0;
        if(grid == null || grid.length == 0 || grid[0].length == 0){
            return 0;
        }
        int height = grid.length;
        for(int i = 0;i < height;i++){
            for(int j = 0;j < grid[i].length;j++){
                if(grid[i][j] == 1){
                    count+=4;
                    if(i > 0 && grid[i-1][j] == 1){ count-=2;}
                    if(j > 0 && grid[i][j-1] == 1){ count-=2;}
                }
            }
        }
        return  count;

    }
}
